package com.ruoyi.web.controller.utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/getCookie.do")
public class gainCookie extends HttpServlet{
    @Override
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doPost(req, resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取Cookie对象
            Cookie[] cookies = req.getCookies();
            //获取Cookie中的数据
            PrintWriter pw = resp.getWriter();
            //遍历Cookie数组
            for(int i=0;i<cookies.length;i++) {
                Cookie cookie = cookies[i];
                String name = cookie.getName();
                String value = cookie.getValue();
                pw.println("Name=" + name + ",Value=" + value);
            }
            pw.flush();
            pw.close();
        }
    }
